#include <iostream>

#include <memory>
#include <string>
using namespace std;

#include <grpc++/grpc++.h>

#include "grpc_test.grpc.pb.h"
#include "grpc_test.pb.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using grpc_testPack::grpc_testRequest;
using grpc_testPack::grpc_testResponse;
using grpc_testPack::grpc_testServiceCls;

class grpc_testServiceClsImpl : public grpc_testServiceCls::Service {
public:
  grpc_testServiceClsImpl() {}
  Status grpc_test(ServerContext *context, const grpc_testRequest *request,
               grpc_testResponse *response) override {
    cout << request->req() << endl;
    response->set_res(request->req());
    return Status::OK;
  }
};

void RunServer() {
  std::string server_address("0.0.0.0:50051");
  grpc_testServiceClsImpl service;

  ServerBuilder builder;
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  builder.RegisterService(&service);
  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;

  server->Wait();
}

int main(int argc, char **argv) {
  RunServer();

  return 0;
}
